/*
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-09-12 10:40:26
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-09-20 14:23:37
 * @FilePath: \郭招娣\hou\app\controller\home.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
/*
 * @Author: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @Date: 2022-09-12 10:40:26
 * @LastEditors: 郭招娣 10615594+guo-zhaodi@user.noreply.gitee.com
 * @LastEditTime: 2022-09-14 11:52:32
 * @FilePath: \郭招娣\hou\app\controller\home.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
'use strict';

const Controller = require('egg').Controller;
const jwt = require('jsonwebtoken');
class HomeController extends Controller {
  
  async login() {
    const { ctx } = this;
    const { username, password } = ctx.request.body;
    const list = await this.app.mysql.get('user', { username, password });
    // const list = await this.app.mysql.select('user');
    const token = jwt.sign({ username, password }, 'gzd')
    if (list) {
      ctx.body = {
        code: 200,
        msg: '登录成功',
        author: list.meta,
        username:list.username,
        password:list.password,
        email:list.email,
        id:list.id,
        token
      }
    } else {
      ctx.body = {
        code: 404,
        msg: '登录失败',
      }
    }
  }
  // 注册
  async sign() {
    const { ctx } = this;
    const { username, password, meta } = ctx.request.body;
    const result = await this.app.mysql.get('user', { username });
    if (result) {
      // 账号存在
      ctx.body = {
        code: 500,
        msg: '账号已存在'
      }
    } else {
      // 账号不存在
      const list = await this.app.mysql.insert('user', { username, password, meta:'USER' });
      if (list) {
        ctx.body = {
          code: 200,
          msg: '注册成功',
          list
        }
      }else{
        ctx.body = {
          code: 500,
          msg: '注册失败',
        }
      }

    }
    // const list = await this.app.mysql.get('user', { username, password });
    // // const list = await this.app.mysql.select('user');
    // const token = jwt.sign({ username, password }, 'gzd')
    // if (list) {
    //   ctx.body = {
    //     code: 200,
    //     msg: '登录成功',
    //     author: list.meta,
    //     token
    //   }
    // } else {
    //   ctx.body = {
    //     code: 404,
    //     msg: '登录失败',
    //   }
    // }
  }
}

module.exports = HomeController;
